package com.fairytask.engine.components.text
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.managers.ResManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * 输入文本类。  可设定皮肤，文本左,上偏移, 文字颜色
	 * @author Hanjy
	 */
	public class Input extends BaseComponent
	{
		private var _htmlStr	:String	=	"";
		private var _inputTxt	:TextField;
		private var _tf		:TextFormat;
		
		private var _left		:uint	=	0;
		private var _top		:uint	=	0;
		private var _multiline	:Boolean	=	false;
		private var _wordWrap	:Boolean	=	false;
		
		private var _displayAsPassword:Boolean	=	false;
		private var _editable	:Boolean	=	true;
		
		private var _color:String		=	"ffffff";
		private var _align		:String	=	TextFieldAutoSize.NONE;
		
		/**
		 * 
		 * @param w 文本区域宽度
		 * @param h 文本区域高度
		 * @param _skin 皮肤String
		 * @param l 左偏移
		 * @param t 上偏移
		 * @param c 颜色
		 */
		public function Input(w:uint,h:uint,_skin:String=null, l:uint=0,t:uint=0,c:uint= 0xffffff)
		{
			if(_skin!=null)
			{
				var bgSkin:Sprite	=	ResManager.newResClass(_skin) as Sprite;
				bgSkin.width	=	w;
				bgSkin.height	=	h;
				addChild(bgSkin);
			}
			_tf = new TextFormat();
			_inputTxt			=	new TextField();
			_inputTxt.displayAsPassword	=	_displayAsPassword;
			_inputTxt.type		=	TextFieldType.INPUT;
			_inputTxt.width		=	w;
			_inputTxt.height	=	h;
			_inputTxt.textColor	=	uint("0x"+_color);
			addChild(_inputTxt);
			left	=	l;
			top		=	t;
			
			_inputTxt.addEventListener(Event.CHANGE, onTextChange);
		}
		
		/** Dispatched when any loader that the SWFLoader discovered in the subloaded swf dispatches an OPEN event. **/
		[Event(name="change", type="flash.events.Event")]
		private function onTextChange(evt:Event):void
		{
			dispatchEvent(evt);
		}
		/**
		 * 上偏移量设置
		 * @param value
		 */
		public function set top(value:uint):void
		{
			_top = value;
			_inputTxt.height	-=	value;
			_inputTxt.y	=	value;
		}
		
		public function get top():uint
		{
			return _top;
		}

		/**
		 * 左偏移量设置
		 * @param value
		 */
		public function set left(value:uint):void
		{
			_left = value;
			_inputTxt.width	-=	value;
			_inputTxt.x	=	value;
		}

		public function get left():uint
		{
			return _left;
		}
		
		/**
		 * 
		 * @param value
		 */
		public function set multiline(value:Boolean):void
		{
			_inputTxt.multiline = value;
		}

		/**
		 * 
		 * @param value
		 */
		public function set wordWrap(value:Boolean):void
		{
			_inputTxt.wordWrap = value;
		}

		/**
		 * 
		 * @return 
		 */
		public function get htmlStr():String
		{
			return _inputTxt.text;
		}

		/**
		 * 
		 * @param value
		 */
		public function set htmlStr(value:String):void
		{
			if(value!=null)
			{
				_inputTxt.text = value;
				_inputTxt.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		/**
		 * html格式显示
		 * @param value
		 */
		public function set showHtml(value:String):void
		{
			if(value!=null)
			{
				_inputTxt.htmlText = value;
				_inputTxt.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		public function get inputTxt():TextField
		{
			return _inputTxt;
		}

		public function set txtColor(val:uint):void
		{
			_inputTxt.textColor	=	val;
		}
		
		public function get maxChars():int {
			return _inputTxt.maxChars;
		}
		public function set maxChars(value:int):void {
			_inputTxt.maxChars = value;
		}
		public function set restrict(value:String):void{
			_inputTxt.restrict=value;
		}
		
		public function set textFormat(textFormat:TextFormat):void{
			_inputTxt.setTextFormat(textFormat);
			_inputTxt.defaultTextFormat = textFormat;
		}
		
		public function get color():String
		{
			return _color;
		}
		
		public function set color(value:String):void
		{
			_color	= value;
			_inputTxt.textColor = uint("0x"+value);
			_inputTxt.htmlText = _inputTxt.htmlText;
		}

		public function get editable():Boolean
		{
			return _editable;
		}
		public function set editable(value:Boolean):void
		{
			_editable = value;
			if(_editable)
			{
				_inputTxt.type=TextFieldType.INPUT;
//				_inputTxt.autoSize=TextFieldAutoSize.CENTER;
			}
			else
			{
				_inputTxt.type=TextFieldType.DYNAMIC;
//				_inputTxt.autoSize=TextFieldAutoSize.CENTER;
			}
			_inputTxt.selectable=_editable;
		}

		public function get displayAsPassword():Boolean
		{
			return _displayAsPassword;
		}

		public function set displayAsPassword(value:Boolean):void
		{
			_displayAsPassword = value;
			_inputTxt.displayAsPassword	=	value;
		}

		/**
		 * 设置输入焦点
		 * @param beginIndex 开始位置索引
		 * @param endIndex   结束位置索引
		 * 
		 */		
		public function onFocus(beginIndex:int,endIndex:int):void
		{
			GameStage.stage.focus = _inputTxt;
			_inputTxt.type		  = TextFieldType.INPUT;
			_inputTxt.selectable  = true;
			_inputTxt.setSelection(beginIndex,endIndex);
		}

		public function get align():String
		{
			return _align;
		}

		public function set align(value:String):void
		{
			_align = value;
			_tf.align = _align;
			_inputTxt.defaultTextFormat = _tf;
//			_inputTxt.autoSize	=	_align;
		}
		
		public function setFilter(filterArr:Array):void
		{
			_inputTxt.filters = filterArr;
		}

		/**
		 * 背景框显示和隐藏 
		 * @param val
		 * 
		 */		
		public function set bgVisible(val:Boolean):void
		{
			this.getChildAt(0).visible = val;
		}

		public function set inputTxt(value:TextField):void
		{
			_inputTxt = value;
		}


	}
}